package com.sleepingrain;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.SeekBar;

/**
 * This class implements fire generator view activity.
 * 
 */
public class FireActivity extends SoundActivity implements
	SeekBar.OnSeekBarChangeListener {

    private FireGenerator fireGen;
    private HissingGenerator hissingGen;
    private CracklingGenerator cracklingGen;
    private CricketGenerator cricketGen;

    private SeekBar sbVolume;
    private SeekBar sbFireIntensity;
    private SeekBar sbHissingVolume;
    private SeekBar sbCracklingVolume;
    private SeekBar sbCricketIntensity;
    private SeekBar sbCricketVolume;
    private SeekBar sbTreble;
    private SeekBar sbBass;

    private static final String SETTINGS_NAME = "FireSettings";
    private static final int DEFAULT_VOLUME = 50;
    private static final int DEFAULT_FIRE_INTENSITY = 20;
    private static final int DEFAULT_HISSING_VOL = 30;
    private static final int DEFAULT_CRACKLING_VOL = 20;
    private static final int DEFAULT_CRICKET_INTENSITY = 0;
    private static final int DEFAULT_CRICKET_VOL = 0;
    // filters
    private static final int DEFAULT_TREBLE = 50;
    private static final int DEFAULT_BASS = 50;

    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	this.setContentView(R.layout.fire);
	this.initSoundUI();

	final SampleLib fireLib = new SampleLib();
	fireLib.loadFromAssets(getAssets(), "Fire", 44100, 1);
	this.fireGen = new FireGenerator(soundBuffer, fireLib);

	final SampleLib hissingLib = new SampleLib();
	hissingLib.loadFromAssets(getAssets(), "Hissing", 44100, 1);
	this.hissingGen = new HissingGenerator(soundBuffer, hissingLib);

	final SampleLib cracklingLib = new SampleLib();
	cracklingLib.loadFromAssets(getAssets(), "Crackling", 44100, 1);
	this.cracklingGen = new CracklingGenerator(soundBuffer, cracklingLib);

	final SampleLib cricketLib = new SampleLib();
	cricketLib.loadFromAssets(getAssets(), "Cricket", 44100, 1);
	this.cricketGen = new CricketGenerator(soundBuffer, cricketLib);

	this.initializeSeekbars();
	// Load settings
	this.loadSettings();

	final Thread fireThread = new Thread(this.fireGen, "fireThread");
	fireThread.start();
	final Thread hissingThread = new Thread(this.hissingGen,
		"hissingThread");
	hissingThread.start();
	final Thread cracklingThread = new Thread(this.cracklingGen,
		"cracklingThread");
	cracklingThread.start();
	final Thread cricketThread = new Thread(this.cricketGen,
		"cricketThread");
	cricketThread.start();
	playBackThread.start();
    }

    /**
     * Initialize the used seek bars.
     */
    protected void initializeSeekbars() {
	this.sbVolume = (SeekBar) this.findViewById(R.id.sbVolume);
	this.sbVolume.setOnSeekBarChangeListener(this);

	this.sbFireIntensity = (SeekBar) this
		.findViewById(R.id.sbFireIntensity);
	this.sbFireIntensity.setOnSeekBarChangeListener(this);

	this.sbHissingVolume = (SeekBar) this
		.findViewById(R.id.sbHissingVolume);
	this.sbHissingVolume.setOnSeekBarChangeListener(this);

	this.sbCracklingVolume = (SeekBar) this
		.findViewById(R.id.sbCracklingVolume);
	this.sbCracklingVolume.setOnSeekBarChangeListener(this);

	this.sbCricketIntensity = (SeekBar) this
		.findViewById(R.id.sbCricketIntensity);
	this.sbCricketIntensity.setOnSeekBarChangeListener(this);

	this.sbCricketVolume = (SeekBar) this
		.findViewById(R.id.sbCricketVolume);
	this.sbCricketVolume.setOnSeekBarChangeListener(this);

	this.sbTreble = (SeekBar) findViewById(R.id.sbTreble);
	this.sbTreble.setOnSeekBarChangeListener(this);

	this.sbBass = (SeekBar) findViewById(R.id.sbBass);
	this.sbBass.setOnSeekBarChangeListener(this);
    }

    /**
     * Load settings from the shared preferences.
     */
    protected void loadSettings() {
	SharedPreferences settings = this
		.getSharedPreferences(SETTINGS_NAME, 0);

	// set setting values to seek bars
	this.sbVolume.setProgress(settings.getInt("volume",
		FireActivity.DEFAULT_VOLUME));
	this.sbFireIntensity.setProgress(settings.getInt("intensityFire",
		FireActivity.DEFAULT_FIRE_INTENSITY));
	this.sbHissingVolume.setProgress(settings.getInt("volumeHissing",
		FireActivity.DEFAULT_HISSING_VOL));
	this.sbCracklingVolume.setProgress(settings.getInt("volumeCrackling",
		FireActivity.DEFAULT_CRACKLING_VOL));
	this.sbCricketVolume.setProgress(settings.getInt("volumeCricket",
		FireActivity.DEFAULT_CRICKET_INTENSITY));
	this.sbCricketIntensity.setProgress(settings.getInt("intensityCricket",
		FireActivity.DEFAULT_CRICKET_VOL));
	this.sbTreble.setProgress(settings.getInt("treble", DEFAULT_TREBLE));
	this.sbBass.setProgress(settings.getInt("bass", DEFAULT_BASS));

	// set setting values to sound generators
	this.fireGen.config_setVolume(sbFireIntensity.getProgress());
	this.hissingGen.config_setIntensity(sbFireIntensity.getProgress());
	this.hissingGen.config_setVolume(sbHissingVolume.getProgress());
	this.cracklingGen.config_setIntensity(sbFireIntensity.getProgress());
	this.cracklingGen.config_setVolume(sbCracklingVolume.getProgress());
	this.cricketGen.config_setVolume(sbCricketVolume.getProgress());
	this.cricketGen.config_setIntensity(sbCricketIntensity.getProgress());
    }

    /**
     * Save settings to the shared preferences.
     */
    protected void saveSettings() {
	SharedPreferences settings = this
		.getSharedPreferences(SETTINGS_NAME, 0);
	SharedPreferences.Editor editor = settings.edit();
	editor.putInt("volume", this.sbVolume.getProgress());
	editor.putInt("intensityFire", this.sbFireIntensity.getProgress());
	editor.putInt("volumeHissing", this.sbHissingVolume.getProgress());
	editor.putInt("volumeCrackling", this.sbCracklingVolume.getProgress());
	editor.putInt("volumeCricket", this.sbCricketVolume.getProgress());
	editor.putInt("intensityCricket", this.sbCricketIntensity.getProgress());
	editor.putInt("treble", this.sbTreble.getProgress());
	editor.putInt("bass", this.sbBass.getProgress());
	editor.commit();
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
	    boolean fromTouch) {
	switch (seekBar.getId()) {
	case R.id.sbVolume:
	    this.setVolume(progress);
	    break;
	case R.id.sbFireIntensity:
	    this.fireGen.config_setVolume(progress);
	    this.hissingGen.config_setIntensity(progress);
	    this.cracklingGen.config_setIntensity(progress);
	    break;
	case R.id.sbHissingVolume:
	    this.hissingGen.config_setVolume(progress);
	    break;
	case R.id.sbCracklingVolume:
	    this.cracklingGen.config_setVolume(progress);
	    break;
	case R.id.sbCricketIntensity:
	    this.cricketGen.config_setIntensity(progress);
	    break;
	case R.id.sbCricketVolume:
	    this.cricketGen.config_setVolume(progress);
	    break;
	case R.id.sbBass:
	    this.setBass(progress);
	    break;
	case R.id.sbTreble:
	    this.setTreble(progress);
	    break;
	}

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
	this.showToast(seekBar.getProgress() + " %");
    }

    @Override
    public void startPlayer() {
	ivIcon.setImageResource(R.drawable.ic_pause);
	super.startPlayer();
    }

    @Override
    public void stopPlayer() {
	ivIcon.setImageResource(R.drawable.ic_play);
	super.stopPlayer();
    }

    @Override
    protected void onPause() {
	super.onPause();
	saveSettings();
    }
}
